
package com.starshineworks.util.actions;

import com.starshineworks.entities.EntityE;
import com.starshineworks.entities.TeamE;
import com.starshineworks.entities.UserE;
import com.starshineworks.util.Util;
//import com.starshineworks.util.FileUploader;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ActionManageTeam extends Action {  
       
    @Override
    public boolean perform(HttpServletRequest  request,
                           HttpServletResponse response,
                           HttpServlet         serv,
                           boolean             isMultiPart)
    {   
        Map<String,String[]> map = request.getParameterMap();
        HttpSession sess = request.getSession();
        if (sess != null) {
            Integer uId = (Integer) sess.getAttribute("uId");
            if (uId != null) {
                Integer tId = EntityE.create("TeamE", map);
                /* XXX
                boolean applyChanges = request.getParameter("applychanges") != null;
                boolean requestField = request.getParameter("requestfield") != null;
                
                if (applyChanges) {*/
                    // Set manager
                    List<EntityE> list = EntityE.retrieve(new TeamE(tId));
                    TeamE t = (TeamE) list.get(0);//TeamE.retrieve(new TeamE(tId)).get(0);
                    Map<String, String[]> m = new HashMap<String, String[]>();
                    String[] mg = {uId.toString(), null, null, null};
                    m.put("_manager", mg);
                    TeamE.updateObjectFields(t, m);
                    return true;
                    /* XXX
                }
                if (requestField) {
                    try {
                        response.sendRedirect(null);
                        SswUtil.showForm(request, response, "TeamPage", true, false, true);
                        return true;
                    }
                    catch (Exception e) {   
                    *             Util.handleException(e);
                        return false;
                    }
                }*/
            }
        }
        return false;
    }
    
    @Override
    public String getNextPage() {
        return "Teams";
    }

    @Override
    public String getError() {
        return "Team info cannot be updated or imported.";
    }

}
